import { mapState, mapMutations } from 'vuex'
import Vue from 'vue'

export default {
  computed: {
    ...mapState(['lang', 'userInfo'])
  },
  data () {
    return {
      queryDialogComp: null,
      queryDialogCompView: false,
      prodId: '',
      anchorId: ''
    }
  },
  created () {
    window.addEventListener('message', (eve) => {
      this.queryStringUpdate(eve.data.cmd, eve.data.params)
    })
  },
  beforeDestroy () {
    window.removeEventListener('message', () => {});
  },
  methods: {
    ...mapMutations(['isShowLogin']),
    queryDialogCompClose () {
      this.queryDialogCompView = false
    },
    importDialog (path, name) {
      import(path + '.vue').then(module => {
        // 加载弹出框模块
        let tempModule = Vue.extend(module.default)
        Vue.component(`dialog-${name}`, tempModule)
        this.queryDialogComp = `dialog-${name}`
        this.queryDialogCompView = true
      })
    },
    queryStringUpdate (cmd, params) {
      let isMobile = this.$route.path.indexOf('/wap') >= 0
      // let queryString = this.queryString
      // let articleid = queryString.articleid // 1 历史墙  2 循环经济  3 欢迎页 
      // let usercenter = queryString.usercenter // 个人中心
      let baseUrl = `/foundation/${isMobile?'wap':'pc'}/`

      // 1 历史墙  2 循环经济  3 欢迎页 
      let lang = this.lang == 'zh' ? 'Cn' : 'En'
      if (cmd == 'articleid' && params == '1') {
        // this.importDialog('./' + 'productInnovations'+lang, 'productInnovations'+lang)
        // 注: 改成接口获取数据
        this.anchorId = 'F2-W-17.1' // 锚点写死
        this.importDialog('./' + 'historyWall', 'historyWall')
      } else if (cmd == 'articleid' && params == '2') {
        // this.importDialog('./' + 'economy'+lang, 'economy'+lang)
        // 注: 改成接口获取数据
        this.anchorId = 'F2-W-17.2' // 锚点写死
        this.importDialog('./' + 'historyWall', 'historyWall')
      } else if (cmd == 'articleid' && params == '3') {
        this.importDialog('./' + 'receptionCenter'+lang, 'receptionCenter'+lang)
      }

      if (cmd == 'prodId' && params) {
        if (!isMobile) {
          this.prodId = params
        } else {
          this.$router.push({
            path: baseUrl+'detail/'+params
          })
        }
      }
      // video 
      if (cmd == 'videolist') {
        this.$router.push({
          path: baseUrl+'video'
        })
      }

      // 个人中心
      if (cmd == 'usercenter' && params == '1') {
        if (!this.userInfo) {
          if (!isMobile) {
            this.isShowLogin('show')
          } else {
            this.$router.replace('/foundation/wap/login')
          }
          return
        }
        this.$router.push({
          path: baseUrl+'info'
        })
      }
    }
  }
}